package com.joolun.activity.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joolun.activity.domain.ActivityInfo;
import com.joolun.activity.domain.ActivityTake;
import com.joolun.activity.domain.ActivityTakeInfo;
import com.joolun.activity.vo.ShareInfoVo;
import com.joolun.common.core.domain.entity.SysUser;
import com.joolun.common.utils.DateUtils;
import com.joolun.common.utils.GeneratorUtils;
import com.joolun.common.utils.SecurityUtils;
import com.joolun.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.joolun.activity.mapper.ShareInfoMapper;
import com.joolun.activity.domain.ShareInfo;
import com.joolun.activity.service.IShareInfoService;

/**
 * 年味分享Service业务层处理
 *
 * @author www.joolun.com
 * @date 2023-01-18
 */
@Service
public class ShareInfoServiceImpl extends ServiceImpl<ShareInfoMapper, ShareInfo> implements IShareInfoService
{
    @Autowired
    private ShareInfoMapper shareInfoMapper;

    /**
     * 查询年味分享
     *
     * @param id 年味分享ID
     * @return 年味分享
     */
    @Override
    public ShareInfo selectShareInfoById(String id)
    {
        return shareInfoMapper.selectShareInfoById(id);
    }

    /**
     * 查询年味分享列表
     *
     * @param shareInfo 年味分享
     * @return 年味分享
     */
    @Override
    public List<ShareInfo> selectShareInfoList(ShareInfo shareInfo)
    {
        return shareInfoMapper.selectShareInfoList(shareInfo);
    }

    /**
     * 新增年味分享
     *
     * @param shareInfo 年味分享
     * @return 结果
     */
    @Override
    public int insertShareInfo(ShareInfo shareInfo)
    {
        shareInfo.setCreateTime(DateUtils.getNowDate());
        return shareInfoMapper.insertShareInfo(shareInfo);
    }

    /**
     * 修改年味分享
     *
     * @param shareInfo 年味分享
     * @return 结果
     */
    @Override
    public int updateShareInfo(ShareInfo shareInfo)
    {
        shareInfo.setUpdateTime(DateUtils.getNowDate());
        return shareInfoMapper.updateShareInfo(shareInfo);
    }

    /**
     * 批量删除年味分享
     *
     * @param ids 需要删除的年味分享ID
     * @return 结果
     */
    @Override
    public int deleteShareInfoByIds(String[] ids)
    {
        return shareInfoMapper.deleteShareInfoByIds(ids);
    }

    /**
     * 删除年味分享信息
     *
     * @param id 年味分享ID
     * @return 结果
     */
    @Override
    public int deleteShareInfoById(String id)
    {
        return shareInfoMapper.deleteShareInfoById(id);
    }

    @Override
    public void insertShareInfoVo(ShareInfoVo shareInfoVo) {
        //查询当前用户是否已经分享
        ShareInfo shareInfo= getOne(new QueryWrapper<ShareInfo>()
                .eq("open_id",shareInfoVo.getOpenId())
                .eq("status","1")
        );
        if(shareInfo!=null){
            throw new RuntimeException("当前用户已分享，正在审核中");
        }

        shareInfo= getOne(new QueryWrapper<ShareInfo>()
                .eq("open_id",shareInfoVo.getOpenId())
                .eq("status","2")
        );
        if(shareInfo!=null){
            throw new RuntimeException("当前用户已分享，不能重复分享");
        }

        shareInfo=new ShareInfo();
        shareInfo.setShareCode(GeneratorUtils.getSecurityCode(8, GeneratorUtils.SecurityCodeLevel.Hard,false).toUpperCase());
        shareInfo.setName(shareInfoVo.getName());
        shareInfo.setImage(shareInfoVo.getImage());
        shareInfo.setOpenId(shareInfoVo.getOpenId());
        shareInfo.setIntro(shareInfoVo.getIntro());
        shareInfo.setPhone(shareInfoVo.getPhone());
        shareInfo.setStatus("1");
        shareInfo.setDelFlag("0");
        shareInfo.setNum(0L);
        shareInfo.setCreateTime(DateUtils.getNowDate());
        if(StringUtils.isEmpty(shareInfoVo.getName())){
            throw new RuntimeException("作品名称为空");
        }
        if(StringUtils.isEmpty(shareInfoVo.getImage())){
            throw new RuntimeException("作品图片为空");
        }
        save(shareInfo);
    }
}
